<?php
	header('Content-Type:text/html;Charset=utf-8');
	header("Access-Control-Allow-Origin:*");
	header("Access-Control-Allow-Methods:GET,POST");	
	// 用系统变量_POST拿到表单提交的数据
	$name=$_POST["name"];
	$life=$_POST["life"];
	$level=$_POST["level"];
	$score=$_POST["score"];
	$pClass=$_POST["pClass"];
	//(1)连接process数据库
	$conn=mysqli_connect('localhost:3306','root','sdo2605587','process');
	if (mysqli_connect_error()) {
    die("数据库连接失败: " . mysqli_connect_error());
	}
	//为数据库指定字符集
	mysqli_query($conn,'SET NAMES utf8');

	//查询是否有相关数据
	$sqls="SELECT * FROM fly WHERE name='$name'";
	//修改相关数据
	$sqlu="UPDATE fly SET life='$life',score='$score',level='$level',pClass='$pClass' WHERE name='$name'";
	//插入相关数据
	$sqli="INSERT INTO fly (dno,name,life,score,level,pClass) VALUES (null,'$name','$life','$score','$level','$pClass')";
	
	$results=mysqli_query($conn,$sqls);
	//如果有，则进行比较大小
	if(mysqli_num_rows($results)){
		$row=mysqli_fetch_assoc($results);
		if($score > $row['score']){
			//如果超过分数进行修改操作
			$resultu=mysqli_query($conn,$sqlu);
			if($resultu){
				echo "修改成功";
			}else{
				echo "修改失败";
			}
		}else{
			//如果没有超过分数直接返回值不操作
			echo "没超";
		};
	//如果没有，则进行插入
	}else{
		$resulti=mysqli_query($conn,$sqli);
		if($resulti){//执行成功后，读取上述SQL语句影响的行数
			echo $affectedRows;
			echo "插入成功";
		}else{
			echo "插入失败";
		}
	};
?>